package com.jsp.app.controller;

import com.tamll.Type.DataType;
import com.tamll.bean.Category;
import com.tamll.bean.Product;
import com.tamll.bean.ProductImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Li_ForeProductDetailsController {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 商品详情
     * @param productId 商品ID
     * @return 将数据传输到页面上
     */
    @ResponseBody
    @GetMapping("detailsOfGoods")
    public ModelAndView detailsOfGoods(@RequestParam(defaultValue = "1") Integer productId, HttpSession session){

//        商品页面搜索栏下面的三个分类信息
        DataType dataType=new DataType();

//        查询所有分类信息  只取前三个
        DataType forObject = restTemplate.getForObject("http://localhost:9092/sortAll", DataType.class);

        List<Category> list=new ArrayList<>();


        for (int i = 0; i < forObject.getData().size(); i++) {
            list.add(forObject.getData().get(i));
            if(i==2){
                break;
            }
        }
        dataType.setThreeDataCategory(list);
//       搜索框下面的三个分类信息  结束




//        五张图片信息
        String url="http://localhost:9092/productimageProductIdSelect?productId={productId}";
        Map<String,Integer> map=new HashMap<>();
        map.put("productId",productId);
        DataType forObject1 = restTemplate.getForObject(url,DataType.class,map);

        List<ProductImage> image = new ArrayList<>();

        for (int i = 0; i < forObject1.getImage().size(); i++) {
            image.add(forObject1.getImage().get(i));
            if(i==4){
                break;
            }
        }
        dataType.setImage(image);
//        五张图片信息结束



        //根据id查询商品信息
        String url1="http://localhost:9092/selectProductId?ProductId={productId}";
        Map<String,Integer> map1=new HashMap<>();
        map1.put("productId",productId);
        DataType forObject2 = restTemplate.getForObject(url1,DataType.class,map1);

        dataType.setProduct(forObject2.getProduct());
//        结束



//       同类产品推荐
        String url2="http://localhost:9092/titleCategoryId?categoryId={categoryId}";
        Map<String,Integer> map2=new HashMap<>();
        map2.put("categoryId",forObject2.getProduct().getProductCategoryId());
        DataType forObject3 = restTemplate.getForObject(url2, DataType.class, map2);
        dataType.setProducts(forObject3.getProducts());

        for (Product product:forObject3.getProducts()) {
//            根据商品id 查询商品图片
            String url3="http://localhost:9092/productimageProductIdSelect?productId={productId}";
            Map<String,Integer> map3=new HashMap<>();
            map3.put("productId",product.getProductId());
            DataType forObject4 = restTemplate.getForObject(url3, DataType.class, map3);
            product.setSingleProductImageList(forObject4.getImage());
        }

//        结束


//      根据商品id 查询分类名称
        String url4="http://localhost:9092/selectCategoryName?productId={productId}";
        Map<String,Integer> map4=new HashMap<>();
        map4.put("productId",productId);
        Category forObject5 = restTemplate.getForObject(url4, Category.class, map4);
        dataType.setCategory(forObject5);

//        结束


//        根据商品id 查询商品分类id
        String url7="http://localhost:9092/getProduct?productId={productId}";
        Map<String,Integer> map7=new HashMap<>();
        map7.put("productId",productId);
        Product forObject7 = restTemplate.getForObject(url7, Product.class, map7);

//        产品规格
        String url5="http://localhost:9093/specificationParameter?productId={productId}&propertyCategoryId={propertyCategoryId}";
        Map<String,Integer> map5=new HashMap<>();
        map5.put("productId",productId);
        map5.put("propertyCategoryId",forObject7.getProductCategoryId());
        DataType forObject4 = restTemplate.getForObject(url5, DataType.class, map5);
        dataType.setParameterList(forObject4.getParameterList());
//        评论
        String url6="http://localhost:9093/commentList?productId={productId}";
        Map<String,Integer> map6=new HashMap<>();
        map6.put("productId",productId);
        DataType forObject6 = restTemplate.getForObject(url6, DataType.class, map6);
        dataType.setReviewList(forObject6.getReviewList());
        ModelAndView mav=new ModelAndView();
        mav.setViewName("fore/productDetailsPage");
        mav.addObject("categoryList",dataType);
        session.setAttribute("categoryList",dataType);
        return mav;
    }






}
